import type { Effect, Reducer } from 'umi';

type Deal = {
  blockId: string;
  createTime: number;
  dealHash: string;
  dealId: string;
  dealType: number;
  fileHash: string;
  state: number;
  blockHeight: number;
  orderType: string;
  relId: string;
  relServiceName: string;
  dealTime: string;
  relState: string;
};

export type CardDetail = {
  blockHash: string;
  blockHeight: number;
  createTime: number;
  dealHash: Deal;
  id: string;
  preHash: string;
  preId: string;
  relState: string;
};

export type BlockchainDetailState = {
  list: any[];
  data: RecordItem;
};

interface BlockchainDetailModal {
  namespace: string;
  state: BlockchainDetailState;
  effects: {
    getAccountingHashFind: Effect;
    getFindOrderAnnData: Effect;
  };
  reducers: {
    updateState: Reducer<BlockchainDetailState>;
  };
}

const BlockchainDetail: BlockchainDetailModal = {
  namespace: 'blockchainDetail',
  state: {
    list: [],
    data: {},
  },
  effects: {
    *getAccountingHashFind({ payload }, { call, put }) {
      const res: RecordItem = yield call(payload);

      yield put({
        type: 'updateState',
        payload: {
          list: res,
        },
      });
    },
    *getFindOrderAnnData({ payload }, { call, put }) {
      const res: string = yield call(payload);
      yield put({
        type: 'updateState',
        payload: {
          data: JSON.parse(res),
        },
      });
    },
  },
  reducers: {
    updateState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};

export default BlockchainDetail;
